home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / Kibitz⁄THINK C / DoCursor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-21  |  7.0 KB  |  266 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:        Kibitz
  5. ** File:        docursor.c
  6. ** Written by:  Eric Soldan
  7. **
  8. ** Copyright © 1990-1991 Apple Computer, Inc.
  9. ** All rights reserved.
  10. */
  11.  
  12.  
  13.  
  14. /*****************************************************************************/
  15.  
  16.  
  17.  
  18. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  19. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  20. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  21.  
  22. #ifndef __TEXTEDITCONTROL__
  23. #include "TextEditControl.h"
  24. #endif
  25.  
  26. #ifndef __TOOLUTILS__
  27. #include <ToolUtils.h>
  28. #endif
  29.  
  30. #ifndef __UTILITIES__
  31. #include "Utilities.h"
  32. #endif
  33.  
  34.  
  35.  
  36. /*****************************************************************************/
  37.  
  38.  
  39.  
  40. RgnHandle    gCurrentCursorRgn;
  41.     /* The current cursor region.  The initial cursor region is an empty
  42.     ** region, which will cause WaitNextEvent to generate a mouse-moved
  43.     ** event, which will cause us to set the cursor for the first time.
  44.     */
  45.  
  46. Cursor    *gCurrentCursor, **gCurrentCursorHndl;
  47.     /* The current cursor that applies to gCurrentCursorRgn.  These values
  48.     ** are here to shorten the re-processing time for determining the
  49.     ** correct cursor after an event.  This is specifically so that characters
  50.     ** can be typed into the TextEdit control faster.  If we spend a great
  51.     ** deal of time per-event recalculating the cursor region, text entry for
  52.     ** the TextEdit control slows down considerably.  If you want to override
  53.     ** the time savings because you are changing the cursor directly, either
  54.     ** set gCurrentCursor to nil, or call DoSetCursor to set the cursor.
  55.     ** DoSetCursor simply sets gCurrentCursor to nil, as well as setting
  56.     ** the cursor.
  57.     */
  58.  
  59.  
  60.  
  61. /*****************************************************************************/
  62. /*****************************************************************************/
  63.  
  64.  
  65.  
  66. /* Handle the cursor changes, based on if an AppleEvent is involved, or
  67. ** depending on the location of the cursor.  This also calculates the region
  68. ** where the current cursor resides (for WaitNextEvent).  If the mouse is
  69. ** ever outside of that region, an event would be generated, causing this
  70. ** function to be called, allowing us to change the region to the region
  71. ** the mouse is currently in.  The only other cursor management in this sample
  72. ** is for operations such as pulling down a menu.  Just prior to pulling down
  73. ** the menu, the arrow cursor is set.  This prevents any latencies in cursor
  74. ** update to cause a non-arrow cursor to be used in the menus.  This technique
  75. ** should be carried throughout the application.
  76. */
  77.  
  78. #pragma segment Main
  79. void    DoCursor(Boolean isAppleEvent, long classID)
  80. {
  81.     WindowPtr    window, oldPort;
  82.     WindowPeek    wpeek;
  83.     RgnHandle    rgn1, rgn2, rgn3;
  84.     Rect        boardRct, structRct, teViewRct;
  85.     Point        mouseLoc;
  86.     short        cursorID;
  87.     FileRecHndl    frHndl;
  88.     FileRecPtr    frPtr;
  89.     TEHandle    teHndl;
  90.     Boolean        readOnly, twoPlayer, canMove;
  91.     short        resync, myColor, moveColor;
  92.  
  93.     mouseLoc = GetGlobalMouse();
  94.  
  95.     if ((!gInBackground) && (!IsDAWindow(window = FrontWindow()))) {
  96.  
  97.         if (isAppleEvent) {
  98.  
  99.             /* Handle the cursor for Apple Events. */
  100.  
  101.             switch (classID) {
  102.                 case kAEOpenApplication:    cursorID = oappCursor; break;
  103.                 case kAEOpenDocuments:        cursorID = odocCursor; break;
  104.                 case kAEPrintDocuments:        cursorID = pdocCursor; break;
  105.                 case kAEQuitApplication:    cursorID = quitCursor; break;
  106.                 case kAEAnswer:                cursorID = ansrCursor; break;
  107.                 case 'wait':                cursorID = watchCursor; break;
  108.             }
  109.  
  110.             SetRectRgn(gCurrentCursorRgn, kExtremeNeg, kExtremeNeg,
  111.                                           kExtremePos, kExtremePos);
  112.             DoSetCursor(*GetCursor(cursorID));
  113.         }
  114.  
  115.         else {            /* Handle the cursor for non-AppleEvent situations. */
  116.  
  117.             if (IsAppWindow(window)) {
  118.  
  119.                 if (gCurrentCursor) {
  120.                     if (PtInRgn(mouseLoc, gCurrentCursorRgn)) {
  121.                         SetCursor(*gCurrentCursorHndl);
  122.                         return;
  123.                     }
  124.                 }
  125.  
  126.                 SetEmptyRgn(gCurrentCursorRgn);
  127.                 GetPort(&oldPort);
  128.  
  129.                 if (CTETargetInfo(&teHndl, &teViewRct) == window) {
  130.                     SetPort(window);
  131.                     LocalToGlobalRect(&teViewRct);
  132.                     SetPort(oldPort);
  133.                     RectRgn(gCurrentCursorRgn, &teViewRct);
  134.                     if (PtInRect(mouseLoc, &teViewRct)) {
  135.                         SetCursor(*(gCurrentCursorHndl = GetCursor(ibeamCursor)));
  136.                         return;
  137.                     }
  138.                 }
  139.  
  140.                 rgn1 = NewRgn();
  141.                 rgn2 = NewRgn();
  142.                 rgn3 = NewRgn();
  143.  
  144.                 wpeek = (WindowPeek)window;
  145.                 for (; wpeek; wpeek = wpeek->nextWindow) {
  146.  
  147.                     if (IsAppWindow((WindowPtr)wpeek)) {
  148.  
  149.                         frHndl = (FileRecHndl)GetWRefCon((WindowPtr)wpeek);
  150.                         frPtr  = *frHndl;
  151.                         readOnly  = frPtr->fileState.readOnly;
  152.                         twoPlayer = frPtr->doc.twoPlayer;
  153.                         resync    = frPtr->doc.resync;
  154.                         myColor   = frPtr->doc.myColor;
  155.                         moveColor = WhosMove(frHndl);
  156.  
  157.                         canMove = true;
  158.  
  159.                         if (readOnly)
  160.                             canMove = false;
  161.                         if ((twoPlayer) && (myColor != moveColor))
  162.                             canMove = false;
  163.                         if (GameStatus(frHndl) != kGameContinues)
  164.                             canMove = false;
  165.                         if ((moveColor == WHITE) && ((*frHndl)->doc.compMovesWhite))
  166.                             canMove = false;
  167.                         if ((moveColor == BLACK) && ((*frHndl)->doc.compMovesBlack))
  168.                             canMove = false;
  169.                         if ((resync == kScrolling) || (resync == kResync))
  170.                             canMove = false;
  171.  
  172.                         if (canMove) {
  173.                             boardRct = GlobalBoardRect((WindowPtr)wpeek);
  174.                             RectRgn(rgn1, &boardRct);
  175.                             DiffRgn(rgn1, rgn2, rgn1);
  176.                             UnionRgn(rgn3, rgn1, rgn3);
  177.                         }
  178.                     }
  179.  
  180.                     structRct = GetWindowStructureRect((WindowPtr)wpeek);
  181.                     RectRgn(rgn1, &structRct);
  182.                     UnionRgn(rgn1, rgn2, rgn2);
  183.                 }        /* Assume cursor is over an app content. */
  184.  
  185.                 if (!PtInRgn(mouseLoc, rgn3)) {
  186.                         /* The cursor wasn't over a chessboard after all. */
  187.                     SetRectRgn(rgn1, kExtremeNeg, kExtremeNeg,
  188.                                      kExtremePos, kExtremePos);
  189.                     DiffRgn(rgn1, rgn3, rgn3);
  190.                     gCurrentCursor     = &QD(arrow);
  191.                     gCurrentCursorHndl = &gCurrentCursor;
  192.                     SetCursor(gCurrentCursor);
  193.                 }
  194.                 else SetCursor(*(gCurrentCursorHndl = GetCursor(handCursor)));
  195.  
  196.                 DiffRgn(rgn3, gCurrentCursorRgn, gCurrentCursorRgn);
  197.  
  198.                 DisposeRgn(rgn1);
  199.                 DisposeRgn(rgn2);
  200.                 DisposeRgn(rgn3);
  201.                 return;
  202.             }
  203.  
  204.             else DoSetCursor(&QD(arrow));
  205.         }
  206.     }
  207.  
  208.     else {
  209.         SetRectRgn(gCurrentCursorRgn, kExtremeNeg, kExtremeNeg,
  210.                                       kExtremePos, kExtremePos);
  211.         gCurrentCursor = nil;
  212.     }
  213. }
  214.  
  215.  
  216.  
  217. /*****************************************************************************/
  218.  
  219.  
  220.  
  221. #pragma segment Main
  222. void    DoSetCursor(Cursor *cursor)
  223. {
  224.     if (cursor) SetCursor(cursor);
  225.     gCurrentCursor = nil;
  226.     if (!cursor) DoCursor(false, 0);
  227. }
  228.  
  229.  
  230.  
  231. /*****************************************************************************/
  232.  
  233.  
  234.  
  235. #pragma segment Main
  236. Rect    BoardRect(void)
  237. {
  238.     Rect    boardRct;
  239.  
  240.     SetRect(&boardRct, kBoardHOffset + 2, kBoardVOffset + 2,
  241.              kBoardHOffset + 8 * kBoardSqSize,
  242.              kBoardVOffset + 8 * kBoardSqSize);
  243.     return(boardRct);
  244. }
  245.  
  246.  
  247.  
  248. /*****************************************************************************/
  249.  
  250.  
  251.  
  252. #pragma segment Main
  253. Rect    GlobalBoardRect(WindowPtr window)
  254. {
  255.     Rect    boardRct, windRct;
  256.  
  257.     boardRct = BoardRect();
  258.     windRct  = GetWindowContentRect(window);
  259.  
  260.     OffsetRect(&boardRct, windRct.left, windRct.top);
  261.     return(boardRct);
  262. }
  263.  
  264.  
  265.  
  266.